package mall.controller.mall;

import com.google.gson.*;
import mall.model.Result;
import mall.model.bo.MallAddOrderBO;
import mall.model.bo.MallSendContentBO;
import mall.model.bo.MallSettleAccountsBO;
import mall.model.vo.MallGetOrderByStateVO;
import mall.service.MallOrderService;
import mall.service.MallOrderServiceImpl;
import mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-06-01 21:03
 *
 * */
@WebServlet("/api/mall/order/*")
public class OrderServlet extends HttpServlet {
    private Gson gson = new Gson();
    private MallOrderService mallOrderService = new MallOrderServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/order/", "");
        if ("addOrder".equals(action)) {
            addOrder(request, response);
        } else if ("settleAccounts".equals(action)) {
            settleAccounts(request, response);
        } else if ("sendComment".equals(action)) {
            sendComment(request, response);
        }
    }

    /**
     * 评价
     * 1. 获取请求参数{token: "Yonni", orderId: 1, goodsId: 2, goodsDetailId: 7, content: "很棒哦", score: 100}
     * 2. 处理业务逻辑
     * 3. 作出响应{"code":0}
     * */
    private void sendComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        MallSendContentBO sendContentBO = gson.fromJson(requestBody, MallSendContentBO.class);
        mallOrderService.sendComment(sendContentBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 下单
     * 1. 获取请求数据{cartList: [{id: 4, goodsNum: 1, amount: 9999999}, {id: 5, goodsNum: 1, amount: 999999}]}
     * 2. 处理业务逻辑
     * 3. 作出响应{"code":0}
     * */
    private void settleAccounts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);

        //解析json字符串中键为cartList的值
        JsonObject jsonObject = new JsonParser().parse(requestBody).getAsJsonObject();
        String cartList = jsonObject.get("cartList").toString();

        //获取cartList中的对象集合
        List<MallSettleAccountsBO> settleAccountsBOS = new ArrayList<>();
        JsonElement jsonElement = new JsonParser().parse(cartList);
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        for (JsonElement element : jsonArray) {
            MallSettleAccountsBO settleAccountsBO = gson.fromJson(element, MallSettleAccountsBO.class);
            settleAccountsBOS.add(settleAccountsBO);
        }

        mallOrderService.settleAccounts(settleAccountsBOS);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 立即购买/添加购物车
     * 1. 获取请求参数{token: "Yonni", goodsDetailId: 1, state: 0, num: 1, amount: 9999999}
     *          state = 1表示立即购买； state = 0表示添加购物车
     * 2. 处理业务逻辑
     * 作出响应{"code":0}
     * */
    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        MallAddOrderBO addOrderBO = gson.fromJson(requestBody, MallAddOrderBO.class);
        mallOrderService.addOrder(addOrderBO);
        response.getWriter().println(gson.toJson(Result.ok()));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/order/", "");
        if ("getOrderByState".equals(action)) {
            getOrderByState(request, response);
        } else if ("deleteOrder".equals(action)) {
            deleteOrder(request, response);
        } else if ("pay".equals(action)) {
            pay(request, response);
        } else if ("confirmReceive".equals(action)) {
            confirmReceive(request, response);
        }
    }

    /**
     * 确认收货
     * 1. 获取请求参数id
     * 2. 处理业务逻辑
     * 3. 作出响应{"code":0}
     * */
    private void confirmReceive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        mallOrderService.confirmReceive(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 付款
     * 1. 获取请求参数id
     * 2. 处理业务逻辑
     * 3. 作出响应{"code":0}
     * */
    private void pay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        mallOrderService.pay(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 删除订单
     * */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        mallOrderService.deleteOrder(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 获取个人中心详情
     * 1. 获取请求参数state=0&token=adminn
     * 2. 处理业务逻辑
     * 3. 作出响应
     * */
    private void getOrderByState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String state = request.getParameter("state");
        String token = request.getParameter("token");
        List<MallGetOrderByStateVO> orders = mallOrderService.getOrderByState(state, token);
        response.getWriter().println(gson.toJson(Result.ok(orders)));
    }
}
